from pygame import sprite, transform, image
import inspect
import os


class Card(sprite.Sprite):
    """植物选择卡"""

    sign = "Card-sign"

    height = 80

    def __init__(
        self, initSchedule: int, schedule_speed: int, sunNum: int, imgPath: str
    ) -> None:
        super().__init__()
        self.follow_mouse = False
        """植物跟随"""
        self.sunNum = sunNum
        """种植所需要的阳光数"""
        self.schedule = initSchedule
        """进度"""
        self.schedule_speed = schedule_speed
        """进度速度"""

        self.image = image.load("./PVZ素材/道具/卡槽.png").convert_alpha()
        combined_width, combined_height = self.image.get_size()

        self.image = transform.scale(
            self.image, (60, combined_height * 60 // combined_width)
        )
        combined_width, combined_height = self.image.get_size()

        # 加载两张图片
        image1 = image.load(
            "{}/{}".format(self.get_caller_directory(), imgPath)
        ).convert_alpha()

        image1 = transform.scale(
            image1,
            ((w := (combined_width / 7 * 5)), combined_height * (w / combined_width)),
        )

        # 创建一个新的 Surface 来包含重叠的图像
        image1_rect = image1.get_rect()
        image1_rect.centerx = self.image.get_rect().centerx

        # 将两张图片绘制到这个 Surface 上
        self.image.blit(image1, image1_rect)

        self.rect = self.image.get_rect()
        self.img_copy = self.image.copy()
        self.img_copy_rect = self.img_copy.get_rect()

    def get_caller_directory(self) -> str:
        # 获取调用栈
        stack = inspect.stack()

        # 获取调用该模块的帧
        caller_frame = stack[2]

        # 获取调用者的文件路径
        caller_file = caller_frame.filename

        # 获取调用者文件所在的目录
        return os.path.dirname(os.path.abspath(caller_file))
